Action In its simplest form, where the loop expression is not provided, the loop statement executes its body statements repeatedly, forever. Such a loop can be terminated by a break or return statement, or by the user canceling the script.
In its more complex form, the loop statement is executed as follows:
1. statement1 is executed;
2. expression is evaluated;
As long as expression evaluates to true,
3. the body statements are executed;
4. statement2 is executed;
5. expression is re-evaluated.
Examples loop (w = window.frontmost (); w != ""; w = window.next (w))
{msg (w)}
This loop iterates through all open windows, posting a message with their titles in the Main Window.
loop
if mouse.button () « user pressed the mouse
break
msg ("It's" + clock.now () + "you still haven’t clicked the mouse!")
This loops until the user presses the mouse button, while posting a message of impatience.
Notes • The complex loop form is most appropriate when the three expressions work together to control the loop iterations, as in the first example above.
• statement1 is executed only once; statement2 is executed after each iteration over the loop body.
• If expression initially evaluates to false, the loop body (and statement2) will not be executed even once.
• The action of the complex loop statement is almost identical to the following while statement:
statement1
while expression
statements
statement2
However, a continue statement in the while body would not cause statement2 to be executed before the next loop iteration, as it does with the loop statement.
• For simple counting loops, the for loop construct is simpler and more efficient.